//
//  ShoppingSellerCtr-ext.swift
//  smosky
//
//  Created by zhuxietong on 2018/6/20.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import UIKit
import soapp
import Eelay
import AsyncDisplayKit

extension ShoppingSellerCtr{
    
    open class EmptyCell:JoCellNode{
        let infoL = JoTextNode()
        override open func mo_model_reload() {
//            backgroundColor = .white
            infoL.attributedText = "没有相关内容".loc.ui.center.font13.cl_888.astring
        }
        override open func specGetter() -> _Spec {
            let h = Sheight - ShoppingSellerCtr.top_height - 40
            return .center(["\(h)"], xy: .xy, child: infoL.Spec)
        }
    }
    
    open class MenuContainerNode: ASDisplayNode {
        public var createMenu:()->EeFilterMenu? = {return nil}
        public var menu:EeFilterMenu? = nil
        open override func didLoad() {
            super.didLoad()
            if let _menu = menu
            {
                view.addSubview(_menu)
            }
            else{
                menu = createMenu()
                if let _menu = menu
                {
                    view.addSubview(_menu)
                }
            }
        }
    }
    
    
    open class SellerFilterMenuNode: ASSizeRangeCell {
        open override func sizeRange() -> ASSizeRange {
            return ASSizeRange(min: [Swidth,40], max: [Swidth,40])
        }
        
        
        open override func addLayoutRules() {
            super.addLayoutRules()
            separatorHeight = 2
            separatorColor = UIColor.table_bg_color
        }
        
        var menu:EeFilterMenu{
            get{
                print("=-==============-----00001")
                if let bar = mo.controller?.__extention["reuseID<\(mo.model["reuseID","menu"])>"] as? EeFilterMenu //可复用处理
                {
                    bar.controller = mo.controller
                    print("+++++++++++___++|\(bar)")
//                    bar.mo.model = mo.model
                    return bar
                }else{
                    let bar = createMenu()
                    mo.controller?.__extention["reuseID<\(mo.model["reuseID","menu"])>"] = bar
                    return bar
                }
            }
        }

        open override func mo_model_reload() {
            super.mo_model_reload()
            view.addSubview(menu)

            
//            menu.createMenu = {
//                [weak self] in ()
//
//                return self?.createMenu()
//            }
        }
//        open override func specGetter() -> _Spec {
//            return .inset([Swidth,"\(40)"], .zero, child: menu[Swidth,"\(38)"])
//        }
//
        open func createMenu() -> EeFilterMenu {
            let menu = EeFilterMenu()
            let item1 = FilterOption()
            item1.filterKey = "type_price"
            item1.button.ui.ntitle(title: "分类  ").stitle(title: "分类  ").nimage(UIImage(named: "shop_order_n")).simage(UIImage(named: "shop_order_s")).scolor(hex: "#000").ncolor(hex: "#999").font(size: 13).imagePosition(.right)
            
            let selector = GoodsSellerCateSelector()
            selector.sellerID = self.mo.model["id",""]
            item1.filterSelector = .push(selector)
            
            
            let item2 = FilterOrder()
            item2.button.ui.ncolor(hex: "#070707").ntitle(title: "热度".loc).nimage(UIImage(named: "shop_order_n")).simage(UIImage(named: "shop_order_s")).scolor(hex: "#999").ncolor(hex: "#999").font(size: 13).imagePosition(.right)
            item2.asc = ("热度".loc,"sales","asc",nil)
            //        item2.desc = ("热度  ","time_deliver","-",nil)
            
            
            let item3 = FilterOrder()
            item3.button.ui.ncolor(hex: "#070707").ntitle(title: "价格".loc).nimage(UIImage(named: "lplb_xiala")).scolor(hex: "#999").ncolor(hex: "#999").font(size: 13).imagePosition(.right)
            item3.asc = ("价格".loc,"productprice","asc",UIImage(named: "sc_px"))
            item3.desc = ("价格".loc,"productprice","desc",UIImage(named: "sc_px2"))
            
            menu.cellType = EeSearchController.MenuCell.self
            item1.itemWidth = Swidth/3.0
            item2.itemWidth = Swidth/3.0
            item3.itemWidth = Swidth/3.0
            
            let ctr = mo.controller  as? ShoppingSellerCtr
            menu.collectionViewConfig = {
                cv in
                if let flow = cv.collectionViewLayout as? UICollectionViewFlowLayout
                {
                    flow.minimumInteritemSpacing = 0
                    flow.minimumLineSpacing = 0
                }
            }
            menu.items = [item1,item2,item3]
            menu.observerFilter = {info in
                ctr?.filterValues = info
            }
            
            menu.controller = ctr
            menu.backgroundColor = UIColor(shex: "#fff")
            menu.frame = CGRect(x: 0, y: 0, width: Swidth, height: menu.menu_height)
            return menu
        }
                
    }
    
    

//    class HeaderViewNode: ASDisplayNode {
//        var header:SellerHeader? = nil
//        open override func didLoad() {
//            super.didLoad()
//            let header = SellerHeader(frame: CGRect(x: 0, y: 0, width: Swidth, height: (245.co-TP.topHeight)))
//            view.addSubview(header)
//            self.header = header
//        }
//
//        open override func layout() {
//            super.layout()
//            header?.frame = self.bounds
//        }
//
//    }
    
    open class SellerHeadNode:JoCellNode {
        open var HeaderType:BaseSellerHeader.Type{
            get{
                return TrSellerHeader.self
            }
        }
        
        public var headerHeight:CGFloat = Swidth
        public var isLoad = false
        public var heaer:ASDisplayNode? = nil
        
        
        open override func willLayoutRules() {
            weak var wself = self
            let heaer = ASDisplayNode { () -> UIView
                in
                
                let Htype = wself?.HeaderType ?? BaseSellerHeader.self
                let view =  Htype.init(frame: CGRect(x: 0, y: 0, width: Swidth, height: (ShoppingSellerCtr.top_height-TP.topHeight)))
                return view
            }
            self.heaer = heaer
            
        }
        
        
        open override func mo_model_reload() {
            if let sheader = heaer?.view as? BaseSellerHeader
            {
                sheader.mo.controller = content_controller
                sheader.mo.model = mo.model
            }
            clipsToBounds = false
        }
        open override func specGetter() -> _Spec {
            let hd = heaer ?? ASDisplayNode()
            return .inset([Swidth,"\((ShoppingSellerCtr.top_height-TP.topHeight))"], .zero, child: hd[Swidth,"\((ShoppingSellerCtr.top_height-TP.topHeight))"])
        }
    }
    
    class TrSellerHeadNode: SellerHeadNode {
        override var HeaderType: ShoppingSellerCtr.BaseSellerHeader.Type{
            return TrSellerHeader.self
        }
    }
    

    open class Header: JoView {
        public let headV = UIImageView()
        public let nameL = UILabel()
        public let infoL = UILabel()
        
        public let careBt = UIButton()
        public let careL = UILabel()
        open override func addLayoutRules() {
            let careBk = UIView()
            eelay = [
                [headV,[ee.L.B,[18,-10]],"50",50],
                [nameL,[headV,ee.R.Y,ee.L.Y,[10,-8]],[ee.R,-90]],
                [infoL,[headV,ee.R.Y,ee.L.Y,[10,11]],[ee.R,-90]],
                [careBk,[ee.B.R],"59",59],
            ]
            nameL.ui.font(font: UIFont.systemFont(ofSize: 12, weight: .black)).cl_fff.text("[XX]旗舰店".replaceVars(values: Goods.self_operated_tag))
            infoL.ui.font11.color(hex: "#E46D1E").text("\("综合评分".loc)：4.8")
            headV.ui.holder(type: .grayBack).set(radius: 25, width: 1, color: UIColor(shex: "#fff4"))
//            headV.img_url = __heads.one_str
            
            careBk.eelay = [
                [careBt,[ee.X.Y,[0,-10]]],
                [careL,[ee.X.Y,[0,10]]],
            ]
            careBk.bsui.background(hex: "#DB340D")
            careBt.ui.nimage(UIImage(named: "dp_xq_gzl"))
            careL.ui.center.font11.text(hex: "#fff").text("关注".loc)
        }
        
        open override func mo_model_reload() {
            nameL.text = mo.model["merchname",""]
            headV.img_url = mo.model["logo",""]
            infoL.text = "\("综合评分".loc): "  + mo.model[float:"score",0].print(num: 1)
            
        }
        
        open override func layoutSubviews() {
            super.layoutSubviews()
            addGradientView()
        }
        
        open func addGradientView() {
            if self.subviews.filter({ (one) -> Bool in
                if one is GradientView{return true}
                return false
            }).count > 0
            {
                return
            }
            let v = GradientView(frame: [0,0,self.frame.size.width,80], red: 0.0, green: 0.0, blue: 0.0)
            v.alpha = 1
            insertSubview(v, belowSubview: headV)
        }
        open override var intrinsicContentSize: CGSize{
            return [Swidth,80]
        }
    }
    
    
    open class BaseSellerHeader: JoView {
        
        public var justHeight:CGFloat{
            get{
                let just = self.mo.controller?.topLayoutGuide.length ?? TP.topHeight
                return just
            }
        }
        public var miniHeigth:CGFloat{
            get{
                return self.frame.size.height + justHeight
            }
        }
      
        
        public var heightLayoutConstraint = NSLayoutConstraint()
        
        public let containerView = UIView()
        
        public let imageV = UIImageView()
        
        public var isLoad:Bool = false
        
        open override func mo_set_controller(controller: UIViewController) {
            if !isLoad
            {
                buildStrech()
                isLoad = true
            }
            
        }
        
        open func buildStrech(){
            
        }
        
        open func scrollViewDidScroll(scrollView: UIScrollView) {
            let offsetY = (-scrollView.contentOffset.y) - justHeight
            if offsetY < 0{
                return
            }
            self.heightLayoutConstraint.constant = miniHeigth+offsetY
            self.containerView.layoutIfNeeded()
            containerView.clipsToBounds = offsetY <= 0
        }
        
        
    }
    
    
    open class TrSellerHeader: BaseSellerHeader {
        
        
        public let seller = Header()
        
   
        
        open override func buildStrech() {
            self.backgroundColor = UIColor.white
            
            self.eelay = [
                [containerView,[ee.L.R.B]],
            ]
            
            self.heightLayoutConstraint = NSLayoutConstraint(item: containerView, attribute:.height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: self.miniHeigth)
            self.addConstraint(heightLayoutConstraint)
            containerView.eelay = [
                [imageV,[ee.T.L.B.R]],
                [seller,[ee.L.B.R]],
            ]
            containerView.clipsToBounds = true
        }
      
        
        override open func mo_model_reload() {
       
            seller.mo.model = mo.model
            imageV.img_url = mo.model["banner","0"]            
        }
        

    }

    
    
}
